#include <GameEngine/Gameplay/Objects/object_inventory.h>

#include <GameEngine/Gameplay/Objects/object_container.h>


#ifdef GAMEENGINE_NAMESPACES
namespace GameEngine {
#endif

#ifndef DOXYGEN_IGNORE
#include "object_inventory.moc"
#endif

//=================================================================================


GAMEENGINE_IMPLEMENT_RTTI(ObjectInventory)

//! Default constructor
ObjectInventory::ObjectInventory() : weight_(0), description_(""), Object() {}

//! Copy constructor
/*!
 @param rhs The object to copy
 */
ObjectInventory::ObjectInventory(const ObjectInventory& rhs) :
	weight_(rhs.weight_), description_(rhs.description_), icon_(rhs.icon_), Object(rhs)
{
	set_container(rhs.container_, true);
}

//! Assignment operator
/*!
 @param rhs The object to copy
 */
ObjectInventory& ObjectInventory::operator=(const ObjectInventory& rhs) {
	if ( &rhs != this ) {
		Object::operator=(rhs);
		weight_ = rhs.weight_;
		description_ = rhs.description_;
		icon_ = rhs.icon_;
		set_container(rhs.container_, true);
	}
	return *this;
}

//! Destructor
/*!
 *  If object was in a container, clear the object from the container
 */
ObjectInventory::~ObjectInventory() {
	set_container(nil, true);
}

//! Accessor to the weight of the inventory object (read only)
/*!
 @return The weight of the inventory object
 */
int ObjectInventory::weight() const {
	return weight_;
}

//! Change the weight of the object
/*!
 @param weight The new weight of the object
 @note If the new weight is different from the current weight, the weight_changed() signal
       is emitted
 */
void ObjectInventory::set_weight(int weight) {
	if ( weight_ == weight ) return;
	weight_ = weight;
	emit weight_changed();
}

//! Accessor to the description of the inventory object (read only)
/*!
 @return The description of the inventory object
 */
const GEString& ObjectInventory::description() const {
	return description_;
}

//! Change the description of the object
/*!
 @param description The new description of the object
 @note If the new description is different from the current description, the
       description_changed() signal is emitted
 */
void ObjectInventory::set_description(const GEString& description) {
	if ( description_ == description ) return;
	description_ = description;
	emit description_changed();
}

//! Accessor to the icon of the inventory object (read only)
/*!
 @return The icon of the inventory object
 */
const QImage& ObjectInventory::icon() const {
	return icon_;
}

//! Change the icon of the object
/*!
 @param icon The new icon of the object
 @note If the new icon is different from the current icon, the icon_changed() signal
       is emitted
 */
void ObjectInventory::set_icon(const QImage& icon) {
	if ( icon_ == icon ) return;
	icon_ = icon;
	emit icon_changed();
}

//! Accessor to the container in which the inventory object is located (read only)
/*!
 @return The container in which the inventory object is located (may be nil if the
         inventory object is not in a container)
 */
ObjectContainer* ObjectInventory::container() const {
	return container_;
}

//! Change the container in which the inventory object is located
/*!
 @param container The new container of the object (may be nil if object should no more
                  be located in a container)
 @param notify_container If set to true, the container is automatically notified of the
                         inventory object move, i.e., the inventory object is automatically
						 added to/removed from the container
 @note If the new container is different from the current container, the container_changed()
       signal is emitted
 */
void ObjectInventory::set_container(ObjectContainer* container, bool notify_container) {
	if ( container_ == container ) return;
	if ( notify_container == true ) {
		if ( container_ != nil ) container_->remove(this, false);
		if ( container != nil ) container->add(this, false);
	}
	container_ = container;
	emit container_changed();
}

#ifdef GAMEENGINE_NAMESPACES
} // namespace GameEngine
#endif
